package com.bootdo.coach.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.coach.domain.TabDeviceType;
import com.bootdo.coach.service.TabDeviceService;
import com.bootdo.coach.service.TabDeviceTypeService;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import io.swagger.annotations.ApiOperation;

@RequestMapping("/deviceType/deviceType")
@Controller
public class DeviceTypeController extends BaseController{
	private String prefix = "msg/deviceType";

	@Autowired
	private TabDeviceTypeService deviceTypeService;
	
	@Autowired
	private TabDeviceService deviceService;
	
	
	@RequiresPermissions("deviceType:deviceType:list")
	@GetMapping("list")
	String list(Model model) {
		return prefix + "/list";
	}
	
	@ApiOperation(value="获取产品列表", notes="")
	@ResponseBody
	@GetMapping("/dataList")
	@RequiresPermissions("deviceType:deviceType:list")
	public PageUtils dataList(@RequestParam Map<String, Object> params) {
		// 查询列表数据
        Query query = new Query( params );
		List<TabDeviceType> list = deviceTypeService.list(query);
		int total = deviceTypeService.count(query);
		PageUtils pageUtil = new PageUtils(list, total);
		return pageUtil;
	}

	@RequiresPermissions("deviceType:deviceType:addDeviceType")
	@Log("添加产品")
	@GetMapping("/addDeviceType")
	String addDeviceType(Model model) {
		return prefix + "/add";
	}

	@RequiresPermissions("deviceType:deviceType:updateDeviceType")
	@Log("编辑产品")
	@GetMapping("/updateDeviceType/{id}")
	String updateDeviceType(Model model, @PathVariable("id") Long id) {
		TabDeviceType deviceType = deviceTypeService.selectByPrimaryKey(id);
		model.addAttribute("deviceType", deviceType);
		return prefix+"/edit";
	}

	@RequiresPermissions("deviceType:deviceType:addDeviceType")
	@Log("保存产品")
	@PostMapping("/saveDeviceType")
	@ResponseBody
	R saveDeviceType(TabDeviceType record) {
		record.setCreateTime(new Date());
		if (deviceTypeService.insertSelective(record) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("deviceType:deviceType:updateDeviceType")
	@Log("更新产品")
	@PostMapping("/update")
	@ResponseBody
	R update(TabDeviceType record) {
		List<TabDeviceType> list = deviceTypeService.selectByDeviceTypeName(record.getDeviceTypeName());
		if (list != null) {
			for (TabDeviceType tabDeviceType : list) {
				if (record.getId().longValue() != tabDeviceType.getId().longValue()) {
					return R.error("产品名称已经存在");
				}
			}
		}
		
		if (deviceTypeService.updateByPrimaryKeySelective(record) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("deviceType:deviceType:deleteDeviceType")
	@Log("删除产品")
	@PostMapping("/deleteDeviceType")
	@ResponseBody
	R deleteDeviceType(Long id) {
		// 判断是否有设备在用这个类型
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("deviceTypeId", id);
		if (deviceService.exitDeviceTypeId(params)) {
			return R.error("先把该类型下的设备更换类型");
		}
		
		if (deviceTypeService.deleteByPrimaryKey(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/exitDeviceTypeName")
	@ResponseBody
	boolean exitDeviceTypeName(@RequestParam Map<String, Object> params) {
		return !deviceTypeService.exitDeviceTypeName(params);
	}
}
